package io.chenglicun.modules.clc.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.modules.clc.dao.ClcVoteRecordDao;
import io.chenglicun.modules.clc.dto.ClcVoteOptionDTO;
import io.chenglicun.modules.clc.dto.ClcVoteRecordDTO;
import io.chenglicun.modules.clc.entity.ClcVoteOptionEntity;
import io.chenglicun.modules.clc.entity.ClcVoteRecordEntity;
import io.chenglicun.modules.clc.service.ClcVoteOptionService;
import io.chenglicun.modules.clc.service.ClcVoteRecordService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 投票记录
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2023-11-24
 */
@Service
public class ClcVoteRecordServiceImpl extends CrudServiceImpl<ClcVoteRecordDao, ClcVoteRecordEntity, ClcVoteRecordDTO> implements ClcVoteRecordService {

    @Autowired
    private ClcVoteOptionService voteOptionService;

    @Override
    public QueryWrapper<ClcVoteRecordEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcVoteRecordEntity> wrapper = new QueryWrapper<>();

        Object obj = params.get("voteOptionId");
        wrapper.eq(ObjectUtil.isNotEmpty(obj), "vote_option_id", obj);

        return wrapper;
    }

    @Override
    @Transactional
    public void save(ClcVoteRecordDTO dto) {
        if (dto.getVoteUser() == null) dto.setVoteUser(SecurityUser.getUserId());
        if (dto.getVoteTime() == null) dto.setVoteTime(new Date());

        super.save(dto);

        ClcVoteOptionDTO voteOptionDTO = voteOptionService.get(dto.getVoteOptionId());
        voteOptionDTO.setPoll(voteOptionDTO.getPoll() + 1);

        voteOptionService.update(voteOptionDTO);
    }

    @Override
    @Transactional
    public void saveBatch(List<ClcVoteRecordDTO> list) {
        for (ClcVoteRecordDTO dto : list) {
            this.save(dto);
        }
    }

    @Override
    @Transactional
    public void update(ClcVoteRecordDTO dto) {
        ClcVoteRecordDTO voteRecordDTO = this.get(dto.getId());
        {
            ClcVoteOptionDTO voteOptionDTO = voteOptionService.get(voteRecordDTO.getVoteOptionId());
            voteOptionDTO.setPoll(voteOptionDTO.getPoll() - 1);

            voteOptionService.update(voteOptionDTO);
        }

        super.update(dto);
        {
            ClcVoteOptionDTO voteOptionDTO = voteOptionService.get(dto.getVoteOptionId());
            voteOptionDTO.setPoll(voteOptionDTO.getPoll() + 1);

            voteOptionService.update(voteOptionDTO);
        }
    }

    @Override
    @Transactional
    public void delete(Long[] ids) {
        super.delete(ids);

        List<ClcVoteRecordEntity> voteRecordEntities = baseDao.selectList(new QueryWrapper<ClcVoteRecordEntity>().lambda().in(ClcVoteRecordEntity::getId, Arrays.asList(ids)));

        List<Long> voteOptionIds = voteRecordEntities.stream().map(ClcVoteRecordEntity::getVoteOptionId).collect(Collectors.toList());
        List<ClcVoteOptionDTO> voteOptionDTOS = voteOptionService.getListByIds(voteOptionIds);

        for (ClcVoteOptionDTO voteOptionDTO : voteOptionDTOS) {
            voteOptionDTO.setPoll(voteOptionDTO.getPoll() - 1);
        }
        voteOptionService.updateBatchById(ConvertUtils.sourceToTarget(voteOptionDTOS, ClcVoteOptionEntity.class));
    }
}